#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]

extern crate axplat_aarch64_phytium_pi;

#[cfg(feature = "axstd")]
use axstd::println;

use axhal::fcan::*;
use axhal::time;

#[cfg_attr(feature = "axstd", unsafe(no_mangle))]
fn main() {
    println!("飞腾派 canfd 数据收发演示");

    let mut fcan_system = FCanSystem::new();

    // 获取并初始化 CAN0 控制器
    let fcan0_controller = fcan_system.controller(0).unwrap();

    match fcan0_controller.init() {
        Ok(_) => println!("CAN0 initialized successfully"),
        Err(_) => {
            println!("Failed to initialize CAN0");
            return;
        }
    }

    fcan0_controller.set_enable(false);
    println!("CAN0 disabled for configuration");
    fcan0_controller.set_canfd_enable(true);
    println!("CAN0 CAN FD enabled");
    fcan0_controller.set_can_mode(1); // 设置为正常模式
    println!("CAN0 set to normal mode");

    println!("Starting CAN loopback test...");
    // 设置仲裁段波特率 1000000 (系统时钟50MHz)
    let arb_cfg = FCanBaudrateConfig {
        segment: FCanSegmentType::Arbitration,
        auto_calc: true,
        baudrate: 1_000_000,

        sample_point: 0,
        prop_seg: 0,   // 由自动计算确定
        phase_seg1: 0, // 由自动计算确定
        phase_seg2: 0, // 由自动计算确定
        sjw: 0,        // 由自动计算确定
        brp: 0,        // 由自动计算确定
    };
    fcan0_controller.set_baudrate(&arb_cfg).ok();

    // 设置数据段波特率 1000000 (系统时钟50MHz)
    let dat_cfg = FCanBaudrateConfig {
        segment: FCanSegmentType::Data,
        auto_calc: true,
        baudrate: 1_000_000,
        sample_point: 0,
        prop_seg: 0,   // 由自动计算确定
        phase_seg1: 0, // 由自动计算确定
        phase_seg2: 0, // 由自动计算确定
        sjw: 0,        // 由自动计算确定
        brp: 0,        // 由自动计算确定
    };
    fcan0_controller.set_baudrate(&dat_cfg).ok();
    println!("CAN0 update  baudrate use calc configured ");

    fcan0_controller.set_enable(false);
    for i in 0..4 {
        let id_mask = FCanIdMaskConfig {
            filter_index: i,
            frame_type: FCanFrameType::Standard,
            id: 0x000,
            mask: 0x1FFFFFFF,
        };
        fcan0_controller.set_id_mask_filter(&id_mask).ok();
    }

    fcan0_controller.enable_id_mask_filter();
    println!("CAN0 configured id mask filter");
    fcan0_controller.set_enable(true);
    println!("CAN0 configured for loopback test");
    println!(
        "CAN0 baudrate set: arbitration {}, data {}",
        arb_cfg.baudrate, dat_cfg.baudrate
    );

    loop {
        println!("send Extended format frame");
        if let Err(e) = fcan0_controller.send_test_frame(false) {
            println!("Failed to send Extended format frame: {}", e);
        }

        println!("receive frames");
        if let Err(e) = fcan0_controller.receive_frames() {
            println!("Failed to receive frames: {}", e);
        }

        println!("send Standard format frame");
        if let Err(e) = fcan0_controller.send_test_frame(true) {
            println!("Failed to send Standard format frame: {}", e);
        }

        println!("receive frames");
        if let Err(e) = fcan0_controller.receive_frames() {
            println!("Failed to receive frames: {}", e);
        }

        time::busy_wait(time::Duration::from_secs(1));
    }
}
